﻿using Sidvall.FileSystemManager.UI.Entities;
using System.Threading.Tasks;

namespace Sidvall.FileSystemManager.UI.ViewModels
{
	public class AppStartViewModel : Sidvall.UI.ViewModels.ViewModelBase
	{
		#region Public Members

		#region UserConfiguration

		private UserConfiguration _UserConfiguration;
		public UserConfiguration UserConfiguration
		{
			get
			{
				return _UserConfiguration;
			}
			set
			{
				_UserConfiguration = value;
				RaisePropertyChanged("UserConfiguration");
			}
		}

		#endregion
		#region Version

		private string _Version;
		public string Version
		{
			get
			{
				return _Version;
			}
			set
			{
				_Version = value;
				RaisePropertyChanged("Version");
			}
		}

		#endregion
		#region WorkCaption

		private string _WorkCaption;
		public string WorkCaption
		{
			get
			{
				return _WorkCaption;
			}
			set
			{
				_WorkCaption = value;
				RaisePropertyChanged("WorkCaption");
			}
		}

		#endregion
		#region ProgressValue

		private double _ProgressValue;
		public double ProgressValue
		{
			get
			{
				return _ProgressValue;
			}
			set
			{
				_ProgressValue = value;
				RaisePropertyChanged("ProgressValue");
			}
		}

		#endregion

		#region ShowContent

		protected void ShowContent()
		{
			Sidvall.UI.SystemContext.Current.ApplicationManager.Status = Sidvall.UI.ApplicationStatus.Started;
			ShowViewModel<NavigationViewModel>((object)null, false, "LeftContent");
			switch (this.UserConfiguration.StartPageId)
			{
				case Sidvall.FileSystemManager.Data.PageType.Clean:
					ShowViewModel<CleanConfigViewModel>((object)null, false, "CenterContent");
					break;
				case Sidvall.FileSystemManager.Data.PageType.Search:
					ShowViewModel<SearchConfigViewModel>((object)null, false, "CenterContent");
					break;
				default:
					ShowViewModel<RenameConfigViewModel>((object)null, false, "CenterContent");
					break;
			}
		}

		#endregion

		#region StartAsync

		protected override async Task StartAsync()
		{
			System.Collections.Generic.List<System.Threading.Tasks.Task> tasks;

			tasks = new System.Collections.Generic.List<System.Threading.Tasks.Task>();
			this.ProgressValue = 0;
			this.WorkCaption = "Laddar användarinställningar...";
			tasks.Add(Sidvall.FileSystemManager.UI.SystemContext.Current.LoadUserConfigurationAsync());
			tasks.Add(Sidvall.FileSystemManager.SystemContext.Current.LoadVersionAsync());
			await System.Threading.Tasks.Task.WhenAll(tasks);
			this.UserConfiguration = Sidvall.FileSystemManager.UI.SystemContext.Current.UserConfiguration;
			this.Version = Sidvall.FileSystemManager.SystemContext.Current.Version;

			this.ProgressValue = 90;
			this.WorkCaption = "Kontrollerar version...";
			await Task.Delay(10);

			this.ProgressValue = 100;
		}

		#endregion
		#region OnAfterStart

		protected override void OnAfterStart()
		{
			ShowContent();
		}

		#endregion

		#endregion
	}
}
